package Market;


import sim.engine.*;
import java.util.*;

public class ISO implements Steppable
   {
	private static final long serialVersionUID = 6628525522939315274L;
	
													/**  Node Data : {number of buses, Penalty coefficient} */
	private double[][] NodeData;
													/** Branch Data : {From, To, Branch Capacity, Reactance} */
	private double[][] BranchData;	
													/** Hourly loadProfile Array = fixed demand : { each LSE demand over current hour } */
	private double[]   LoadProfile;
													/** Hourly loadProfile Double Array = fixed demand : { each LSE demand over current hour } */
	private double[][] LoadProfileD;
													/** True supply offer (a,b,CapTL,CapTU)  <br> @see DynTestAMES.JSLT.pdf */
	private double[][] TSupplyOffer; 
													/** Reported supply offer (aR,bR,CapRL,CapRU) <br> @see DynTestAMES.JSLT.pdf*/
	private double[][] RSupplyOffer; 
													/** GenCo's Data: {Gen ID, at Node, FCost, aT, bT, CapTL, CapTU, Initial Money in pounds} */
	private double[][] GenCoData;
													/** LSE's Data: {LSE ID, at Node, L(0), L(1),L(2),L(3),L(4), ...,L(23)} */
	private double[][] LSEData;
	
													/** Number of LSEs */
	int J;
													/** Number of GenCos */
	int I;
													/** Number of Nodes*/
	int K;
													/** Number of Branches*/
	int N;
													/** Current hour*/
	//int hour;
	
	private ArrayList GenCoList;
	
	private Market              market;
	private Initials.InitNode   initnode;
	private Initials.InitBranch initbranch;
	private OptimalPowerFlow    opf;
	
	  public Hashtable<String, Hashtable> reportedSupplyOffer = new Hashtable<String, Hashtable>();
	  public Hashtable<String, Hashtable> electricityDemand = new Hashtable<String, Hashtable>();
	  public Hashtable<String, Hashtable<String, Double>> branchList = new Hashtable<String, Hashtable<String, Double>>() ;
	  public Hashtable<String, Double> nodeData = new Hashtable<String, Double>() ;
	  
//C-O-N-S-T-R-U-C-T-O-R------------------------------------------------------------------------------------------------------------------------------  
	//public ISO(Hashtable reportedsupplyoffer, Hashtable electricitydemand, Hashtable branchlist, Hashtable nodedata,int h, Market mt){
	  public ISO(Market mt){
		//reportedSupplyOffer = reportedsupplyoffer;
	//	electricityDemand   = electricitydemand;
	//	branchList 			= branchlist;
	//	nodeData 			= nodedata;
		
		market = mt;
		I      = mt.I;
		J      = mt.J;
		K      = mt.K;
		N      = mt.N;
	//	hour   = h;
		
		//GenCoData    = new double[I][8];
		//LSEData      = new double[J][26];
		//GenCoData    = gencodata;
		//LSEData      = lsedata;		
	}
	
//-----------------------------------------------------------------------------------------------------------------------------------------
    public void step(SimState state){
    	
    	
    	//System.out.println("Iso1 ");
   	
    	for (int hour=0; hour<market.Hours; hour++){
    		
    	
    	
    	NodeData     = new double[1][2];
    	BranchData   = new double[5][4];
    	LoadProfileD = new double[24][3];
    	LoadProfile  = new double[3];
    	TSupplyOffer = new double[I][4];
    	RSupplyOffer = new double[I][4];
    	GenCoData    = new double[I][8];
    	//initnode     = new Initials.InitNode();
    	//initbranch   = new Initials.InitBranch();   	
    	//NodeData     = initnode.getNodeData();
    	//BranchData   = initbranch.getBranchData();  	
    	//LoadProfileD = market.getLoadProfile();
    	for (int i=0; i<market.I; i++)
    	{
    		//GenCo gen = new GenCo();
    		//gen = market.genCoList.get(Integer.toString(i+1));
    		int id = i+1;
    		GenCo gen = market.genCoList.get("genco"+id);
    		GenCoData[i][0] = id; 
    		GenCoData[i][1] = gen.node;
    		GenCoData[i][2] = gen.fcost;
    		GenCoData[i][3] = gen.supplyOffer.get("aT");
    		GenCoData[i][4] = gen.supplyOffer.get("bT");
    		GenCoData[i][5] = gen.supplyOffer.get("capTL");
    		GenCoData[i][6] = gen.supplyOffer.get("capTU");
    		GenCoData[i][7] = gen.genCoMoney;
    	
    		TSupplyOffer[i][0] = gen.supplyOffer.get("aT");
    		TSupplyOffer[i][1] = gen.supplyOffer.get("bT");
    		TSupplyOffer[i][2] = gen.supplyOffer.get("capTL");
    		TSupplyOffer[i][3] = gen.supplyOffer.get("capTU");   	
    	}
    	
    	
    	for (int j=0; j<market.J; j++)
    	{
    		int id = j+1;
    		LoadProfile[j] = market.lseList.get("lse"+id).electricityDemand.get(Integer.toString(hour));
    	}
    	
		NodeData[0][0] =  market.nodeData.get("buses");
		NodeData[0][1] =  market.nodeData.get("penalty");
		
     	
    	for (int n=0; n<market.N; n++)
    	{
    		int id = n+1;
    		//Initials.InitBranch initbranch = market.branchList.get("branch"+id);
    		
    		Hashtable<String, Double> BranchHash = new Hashtable<String, Double>();
    		BranchHash =(Hashtable) market.branchList.get("branch"+id);
    		    		
    		BranchData[n][0] = ((Double)BranchHash.get("from")).doubleValue();
    		BranchData[n][1] = ((Double)BranchHash.get("to")).doubleValue();
    		BranchData[n][2] = ((Double)BranchHash.get("capacity")).doubleValue();
    		BranchData[n][3] = ((Double)BranchHash.get("reactance")).doubleValue();
    		
    		//BranchData[n][0] = initbranch.branchData.get("from");
    		//BranchData[n][1] = initbranch.branchData.get("to");
    		//BranchData[n][2] = initbranch.branchData.get("capacity");
    		//BranchData[n][3] = initbranch.branchData.get("reactance");
    	

    	}

/*		for (int n=0; n<market.N; n++){
			int id = n+1;
			BranchData[n][0] = market.branchList.get("branch"+id).branchData1.get("from");
		}
*/    	
    	 
    	opf = new OptimalPowerFlow(NodeData, BranchData, LoadProfile, TSupplyOffer, market, GenCoData, LSEData,  hour, I, J, K, N);       
    }
    }
 }